<?php
namespace app\demo\controller;
use think\Controller;
use think\Request;
use think\Db;
class Goods extends Controller{
    public function one(){
        //商品类型
        $goods_type=Db::table('goods_type')->select();
        $this->assign('goods_type',$goods_type);
        //商品分类
        $data=Db::table('category')->select();
        $type=$this->getTree($data);
        $this->assign('data',$type);
        //商品品牌
        $res=Db::table('brand')->select();
        $this->assign('res',$res);
        return view('goods_add');
    }
    //递归
    public function getTree($data,$pid=0,$leve=1){
        static $arr=array();
        foreach($data as $key=>$val){
            if($val['parent_id']==$pid){
                $val['level']=$leve;
                $arr[]=$val;
                unset($data[$key]);
                $this->getTree($data,$val['cat_id'],$leve+1);
            }
        }
        return $arr;
    }
    public function goods_list(){
        return view();
    }
    //商品添加
    public function adds(){
        $data=Request::instance()->param();
        $res=Db::table('good')->insert($data);
        if($res){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }
    public function upload(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('goods_img');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            // 输出 42a79759f284b767dfcb2a0197904287.jpg
            return $info->getFilename();
        }else{
                        // 上传失败获取错误信息
            return $file->getError();
        }
    }

}